﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ConfigurationManager.View.Dialog
{
    public partial class frmProduct : Form
    {
        Model.Product _product;

        public frmProduct()
        {
            InitializeComponent();
            this.CenterToScreen();
            this.Text = "Inclusão de Produto";
        }

        public frmProduct(Model.Product product)
            : this()
        {
            _product = product;
            txtCode.Text = _product.Id.ToString();
            txtName.Text = _product.Name;
            txtDescription.Text = _product.Description;
            this.Text = "Edição de Produto";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (_product == null)
                    _product = new ConfigurationManager.Model.Product();

                _product.Name = txtName.Text;
                _product.Description = txtDescription.Text;
                Model.Product.Save(_product);
                MessageBox.Show("Produto salvo com sucesso!", "Process Manager Configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("Process Manager - Configuration Manager", ex.ToString(), EventLogEntryType.Error);
                //MessageBox.Show("Ocorreu um erro interno, verifique o event viewer para maiores detalhes");
                Program.HandleException(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmProduct_Load(object sender, EventArgs e)
        {

        }


    }
}
